package com.example.util;

import com.example.entity.CarEntity;
import org.apache.ibatis.jdbc.SQL;

import java.security.SecureRandom;
import java.util.Map;

public class IdUtil {
    private static final String IDSTR = "0123456789abcdefghijglmnopqrstuvwxyz";
    public static String getRadomStr()
    {
        SecureRandom secureRandom = new SecureRandom();
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i<10;i++)
        {
            builder.append(IDSTR.charAt(secureRandom.nextInt(IDSTR.length())));
        }
        return builder.toString();
    }

    public static void main(String[] args) {
//        System.out.println(IdUtil.class.getCanonicalName());
//        System.out.println(IdUtil.class.hashCode());
        SQL sql = new SQL().INSERT_INTO("table");
        CarEntity carEntity = new CarEntity();
        carEntity.setId(122L);
        sql.VALUES("id","#{"+carEntity.getId()+"}");
        sql.VALUES("id","#{"+carEntity.getId()+"}");
        sql.VALUES("nullId", "<if test=\"" + carEntity.getBootId() +
                " != null\">#{" + carEntity.getBootId() + "}</if>" + "<if test=\"" +
                carEntity.getBootId() + " == null\">null</if>");
        sql.VALUES("haveId", "<if test=\"" + carEntity.getId() +
                " != null\">#{" + carEntity.getId() + "}</if>" + "<if test=\"" + carEntity.getId() + " == null\">null</if>");
        System.out.println(sql.toString());

        System.out.println("test update by id");
        StringBuilder colStr = new StringBuilder();
        colStr.append("<if test=\"").append("fieldName").append(" != null\">").append("columnName")
                .append(" = #{").append("fieldName").append("},").append("</if>");
        System.out.println(colStr.toString());//感觉update的sql需要想插入一样，null的时候set 其为null
        System.out.println("update  " + "tableName" + " <set> " + colStr.toString() + " </set> where "
                + "id" + "= #{" + "id" + "}");

        System.out.println("test get by id");

        StringBuilder getByid = new StringBuilder();//，判断是不是第一次，第一次不需要
        getByid.append(",").append("columnName").
                append(false? "" : " as \"" + "fieldName" + "\"");
        System.out.println(getByid.toString());
        System.out.println("select " + getByid.toString() + " from " + "tableName"
                + " where " + "id" + "= #{"
                + "id" + "}");

        System.out.println("test batch insert sql");

        SQL mysql = new SQL().INSERT_INTO("tableName");
        StringBuilder myColStr = new StringBuilder();
        StringBuilder dataStr = new StringBuilder();

        for (int i = 0 ;i <2;i++) {

            if (myColStr.length() == 0) {
                myColStr.append("id");
            } else {
                myColStr.append(",").append("name");
            }
            if (dataStr.length() == 0) {
                dataStr.append("<if test=\"item.").append("id").append(" != null\">#{item.")
                        .append("id").append("}</if>").append("<if test=\"item.").append("id").append(" == null\">null</if>");
            } else {
                dataStr.append(",<if test=\"item.").append("name").append(" != null\">#{item.").append("name")
                        .append("}</if>").append("<if test=\"item.").append("name").append(" == null\">null</if>");
            }
        }
        System.out.println(mysql.toString() + " (" + myColStr.toString() + ")" + " VALUES "
                + "<foreach item=\"item\" collection=\"list\" separator=\",\">" +
                "(" +
                dataStr +
                ")</foreach>");


    }
}
